<?php
/**
 * Created by PhpStorm.
 * User: leo
 * Date: 16/4/5
 * Time: 下午6:32
 * Describe:
 */

namespace services\sms;
use \Yii;
use yii\base\Exception;

class SmsSend extends \yii\base\Component
{
    /**
     * @param null|string $className 发送短信的类的名字
     * @return mixed 发送短信类实例
     */
    public static function factory($className = null)
    {
        if ($className == null) {
            #$className = Yii::$app->redis_old->set(Yii::$app->params['defaultSms'], 'XiAoSms');

            $className = Yii::$app->redis_old->get(Yii::$app->params['defaultSms']);
        }

        if(empty($className)){
            throw new Exception('sms class not found.');
        }
        $class = __NAMESPACE__.'\\'.$className;
        $sms =   new $class();
        $config = \yii\helpers\ArrayHelper::merge(
            require(__DIR__ . '/../../common/config/sms.php'),
            require(__DIR__ . '/../../common/config/sms-local.php')
        );
        Yii::configure($sms, $config[$class::shortClassName()]);
        return $sms;
    }
}